package cn.edu.scau.cmi.huangzhiyuan.db.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import cn.edu.scau.cmi.huangzhiyuan.db.dao.StudentDao;
import cn.edu.scau.cmi.huangzhiyuan.db.dao.TeacherDao;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Person;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Student;
import cn.edu.scau.cmi.huangzhiyuan.db.domain.Teacher;
import javafx.collections.ObservableList;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class TeacherPane extends BorderPane {

	public HeaderPane headerPane;
	public BodyPane bodyPane;
	public VBox footerPane;
	private AppendStudentBox appendStudentBox;
	private TeacherInfoPane teacherInfoPane;
	private VBox studentInfoes;

	public TeacherPane() {
		setHeader();
		setBody();
		setFooter();
	}

	private void setHeader() {
		headerPane = new HeaderPane();
		headerPane.removeChoiceBox();
		setTop(headerPane);
	}

	private void setBody() {
		bodyPane = new BodyPane();
		setCenter(bodyPane);
	}

	private void setFooter() {
		teacherInfoPane = new TeacherInfoPane();
		appendStudentBox = new AppendStudentBox();
		studentInfoes = new VBox();

		footerPane = new VBox();
		footerPane.getChildren().add(appendStudentBox);
		footerPane.getChildren().add(teacherInfoPane);
		footerPane.getChildren().add(studentInfoes);

		setBottom(footerPane);
	}
	
	public void setFooterPane(Teacher teacher, List<Person> students) {
		// 放置选择学生列表
		appendStudentBox.setStudents(students);
		// 放置老师个人信息
		teacherInfoPane.setTeacherInfo(teacher);
		// 放置老师指导的学生列表
		Set<Student> stds = teacher.getStudents();
		if(stds != null) {
			System.out.println(stds);
			Iterator<Student> iterator = stds.iterator();
			// 清除原有学生信息
			studentInfoes.getChildren().clear();
			while(iterator.hasNext()) {
				Student student = iterator.next();
				// 添加现有学生
				appendStudentInfo(student, teacher);
			}
		}
	}
	
	/**
	 * 添加指导学生信息到教师信息面板中
	 * @param student 
	 */
	public void appendStudentInfo(Student student, Teacher teacher) {
		StudentInfoPane studentInfoPane = new StudentInfoPane(student.getId(), student.getName(), teacher);
		studentInfoes.getChildren().add(studentInfoPane);
	}
	
	public void setFooterVisible(Boolean canVisit) {
		getBottom().setVisible(canVisit);
	}
	
	public void setAppendStudentBoxList(List<Person> persons) {
		appendStudentBox.setStudents(persons);
	}
	
	public void clearTeacherInfoAttributes() {
		teacherInfoPane.removeAttributes();
	}
	
	public Button getAppendBoxSelectBtn() {
		return appendStudentBox.selectBtn;
	}
	
	public ChoiceBox<Person> getAppendBoxChoiceBox() {
		return appendStudentBox.students;
	}
	
	public Button getTeacherInfoSaveBtn() {
		return teacherInfoPane.saveBtn;
	}
	
	public Button getTeacherInfoDelBtn() {
		return teacherInfoPane.delBtn;
	}

	public TextField getTeacherInfoNameInput() {
		return teacherInfoPane.nameInput;
	}
	
	public TextField getTeacherInfoIdInput() {
		return teacherInfoPane.idInput;
	}

}




class TeacherInfoPane extends HBox {

	public TextField idInput;
	public TextField nameInput;
	public Button saveBtn;
	public Button delBtn;

	public TeacherInfoPane() {
		init();
	}

	private void init() {
		idInput = new TextField();
		nameInput = new TextField();
		saveBtn = new Button("保存");
		delBtn = new Button("删除");

		getChildren().addAll(idInput, nameInput, saveBtn, delBtn);

		setStyle();
	}

	private void setStyle() {
		idInput.setPrefWidth(150);
		nameInput.setPrefWidth(150);
		saveBtn.setPrefWidth(50);
		delBtn.setPrefWidth(50);

		idInput.setEditable(false);
		
//		setVisible(false);
	}

	public void setTeacherInfo(Teacher teacher) {
		System.out.println("setteacherinfo visible true");
		setVisible(true);
		
		idInput.setText(String.valueOf(teacher.getId()));
		nameInput.setText(teacher.getName());
	}

	public void removeAttributes() {
		System.out.println("setteacherinfo visible false");
		setVisible(false);
		
		idInput.clear();
		nameInput.clear();
	}
}




class StudentInfoPane extends HBox {

	private TextField idInput;
	private TextField nameInput;
	private Button delBtn;
	private Teacher teacher;

	public StudentInfoPane(int id, String name, Teacher teacher) {
		idInput = new TextField(String.valueOf(id));
		nameInput = new TextField(name);
		delBtn = new Button("删除");
		this.teacher = teacher;

		getChildren().addAll(idInput, nameInput, delBtn);

		setStyle();
		
		addListener();
	}

	private void setStyle() {
		idInput.setPrefWidth(150);
		nameInput.setPrefWidth(150);
		delBtn.setPrefWidth(100);

		idInput.setEditable(false);
		nameInput.setEditable(false);
	}
	
	private void addListener() {
		StudentDao studentDao = new StudentDao();
		TeacherDao teacherDao = new TeacherDao();
		
		delBtn.setOnAction((event) -> {
			// 获取学生信息
			Student student = new Student();
			student.setId(Integer.valueOf(idInput.getText()));
			student.setName(nameInput.getText());
			// 设置学生导师信息为null
			student.setTeacher(null);
			// 从教师指导学生中，移除该学生
			Set<Student> stds = teacher.getStudents();
			Iterator<Student> iterator = stds.iterator();
			while(iterator.hasNext()) {
				Student std = iterator.next();
				if(std.getId() == student.getId()) {
					stds.remove(std);
					break;
				}
			}
			teacher.setStudents(stds);
			// 调用dao层更新关系
			studentDao.update(student);
			teacherDao.update(teacher);
			// 隐藏选项
			this.setVisible(false);
		});
	}

}


class AppendStudentBox extends HBox {
	
	public ChoiceBox<Person> students;
	public Button selectBtn;
	
	public AppendStudentBox() {
		init();
	}
	
	private void init() {
		students = new ChoiceBox<>();
		selectBtn = new Button("选择");
		
		getChildren().addAll(students, selectBtn);
		
		setStyle();
	}
	
	private void setStyle() {
		students.setPrefWidth(300);
		selectBtn.setPrefWidth(100);
	}
	
	public void setStudents(List<Person> persons) {
		ObservableList<Person> items = students.getItems();
		int selectIndex = students.getSelectionModel().getSelectedIndex();
		items.clear();
		for(int i=0; i<persons.size(); i++) {
			items.add(persons.get(i));
		}
		students.getSelectionModel().select(selectIndex);
	}
	
}